package com.gutai.admin.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gutai.admin.biz.OrderStatusHelper;
import com.gutai.admin.common.entity.dto.OrderRequestParamsDto;
import com.gutai.admin.common.entity.response.Response;
import com.gutai.admin.common.util.ExceptionUtils;
import com.gutai.admin.common.util.ResponseHelper;
import com.gutai.admin.entity.Order;
import com.gutai.admin.entity.OrderGoods;
import com.gutai.admin.service.IOrderService;

/**
 * 
 * 描述：
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2018年11月7日         YC           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author YC
 * @version 1.0
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private IOrderService orderService;

    @CrossOrigin
    @PostMapping("/listAll")
    public Response listAll(@RequestBody OrderRequestParamsDto paramsDto) {
        Integer startPage = paramsDto.getStartPage();
        Integer pageSize = paramsDto.getPageSize();
        if (startPage == null || startPage < 1) {
            startPage = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        try {
            Page<Order> list = orderService.getList(paramsDto);
            return ResponseHelper.buildOk(list.getContent(), list.getTotalElements());
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

//    @CrossOrigin
//    @GetMapping("/selectList")
//    public Response selectList() {
//        try {
//            Set<Order> datas = (Set<Order>) LocalCacheUtil.getElems(Order.class.getName());
//            if (CollectionUtil.isEmpty(datas)) {
//                datas = new HashSet<>(orderService.getAll());
//                LocalCacheUtil.addOrUpd(Order.class.getName(), datas);
//            }
//            List<SelectListDto> selectList = datas.stream().map(data -> {
//                SelectListDto dto = new SelectListDto();
//                dto.setId(data.getId());
//                dto.setName(data.getOrderSn());
//                return dto;
//            }).collect(Collectors.toList());
//            return ResponseHelper.buildOk(selectList);
//        } catch (Exception e) {
//            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
//            return ResponseHelper.buildFail(e.getMessage());
//        }
//    }
    @CrossOrigin
    @GetMapping("/selectOrderStatus")
    public Response selectOrderStatus() {
        try {
            return ResponseHelper.buildOk(OrderStatusHelper.getOrderStatusList());
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/add")
    public Response add(@RequestBody Order entity) {
        try {
            orderService.add(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping("/update")
    public Response update(@RequestBody Order entity) {
        try {
            orderService.update(entity);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/delete")
    public Response delete(Integer id) {
        try {
            orderService.delete(id);
            return ResponseHelper.buildOk();
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail("操作DB失败:" + e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping("/findOrderGoodsByOrderId")
    public Response findOrderGoodsByOrderId(String orderId) {
        try {
            List<OrderGoods> list = orderService.findOrderGoodsByOrderId(Integer.valueOf(orderId));
            Response res = ResponseHelper.buildOk(list);
            res.setMsg(orderId);
            return res;
        } catch (Exception e) {
            logger.error(" --{}", ExceptionUtils.extractStackTrace(e));
            return ResponseHelper.buildFail(e.getMessage());
        }
    }
}
